package com.zbkc.interfaces;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.zbkc.bean.BeifenBean;
import com.zbkc.bean.BeifenDirectoryBean;
import com.zbkc.bean.BeifenFileBean;
import com.zbkc.bean.BeifenVersionBean;
import com.zbkc.bean.FileBoxBean;
import com.zbkc.bean.LoginBean;
import com.zbkc.bean.ShareBean;
import com.zbkc.bean.ShareFileBean;
import com.zbkc.bean.UserMessageBean;
import com.zbkc.http.Config;
import com.zbkc.http.HttpUtil;
import com.zbkc.http.ServerInterface;
import com.zbkc.util.BASE64Util;
import com.zbkc.util.LogUtil;
import com.zbkc.util.PreferenceUtils;

public class SCInterface {
	private static final String COMMON_PARAMETER = "?q="
			+ PreferenceUtils.getString("username") + "&b="
			+ PreferenceUtils.getString("password") + "&u=";

	/**
	 * 登录接口
	 * 
	 * @param paramString1
	 * @param paramString2
	 * @param paramString3
	 * @param paramString4
	 * @return
	 */
	public static LoginBean login(String paramString1, String paramString2,
			String paramString3, String paramString4) {
		String address = Config.SERVEL_ADDRESS + ServerInterface.LOGIN
				+ COMMON_PARAMETER + "&f=" + paramString1 + "&t="
				+ paramString2 + "&w=" + paramString3 + "&n=" + paramString4;
		JSONObject jsonObject = HttpUtil.postReturnJson(address);

		LoginBean loginBean = LoginBean.createFormJSONObject(jsonObject);
		return loginBean;
	}

	// public static String downFile(String paramString1, String paramString2,
	// String paramString3) {
	// return HttpUtil.postDocumentReturnResult(
	// PreferenceUtils.getString("address") + "csmpv4/mb/getf.js"
	// + COMMON_PARAMETER + "&j=" + paramString1 + "&t="
	// + paramString2, paramString3);
	// }

	/**
	 * 获取用户设备列表
	 * 
	 * @param base64Name
	 * @param base64frontPwd
	 * @param base64nextPwd
	 */
	public static List<BeifenBean> getDeviceList() {

		String address = Config.SERVEL_ADDRESS + ServerInterface.USERDEVICELIST
				+ COMMON_PARAMETER;
		JSONObject jsonObject = HttpUtil.postReturnJson(address);

		List<BeifenBean> beans = new ArrayList<BeifenBean>();
		BeifenBean beifenBean;

		if (jsonObject != null) {
			try {
				String otherMessage = jsonObject.getString("e");
				otherMessage = BASE64Util.byteToString(BASE64Util
						.decode(otherMessage));
				LogUtil.e("其他错误信息:" + otherMessage);

				JSONArray jsonArray = jsonObject.getJSONArray("d");
				for (int i = 0; i < jsonArray.length(); i++) {
					JSONObject json = jsonArray.getJSONObject(i);

					beifenBean = new BeifenBean();

					String d6 = json.getString("d6");
					d6 = BASE64Util.byteToString(BASE64Util.decode(d6));
					LogUtil.e("d6:" + d6);

					String d7 = json.getString("d7");
					d7 = BASE64Util.byteToString(BASE64Util.decode(d7));
					LogUtil.e("d7:" + d7);

					// String d4 = json.getString("d4");
					// d4 = BASE64Util.byteToString(BASE64Util.decode(d4));
					// LogUtil.e("d4:" + d4);

					// String d2 = json.getString("d2");
					// d2 = BASE64Util.byteToString(BASE64Util.decode(d2));
					// LogUtil.e("d2:" + d2);

					// 设备id
					String d1 = json.getString("d1");
					d1 = BASE64Util.byteToString(BASE64Util.decode(d1));
					LogUtil.e("d1:" + d1);
					beifenBean.setId(d1);

					// 设备名
					String d3 = json.getString("d3");
					d3 = BASE64Util.byteToString(BASE64Util.decode(d3));
					LogUtil.e("d3:" + d3);
					beifenBean.setDeviceName(d3);
					beans.add(beifenBean);
				}

			} catch (JSONException e) {
				e.printStackTrace(); // To change body of catch statement use
										// File | Settings | File Templates.
			}
		}

		return beans;
	}

	public static void getBeifenDir(String paramString, int paramInt1,
			int paramInt2) {
		JSONObject localJSONObject = HttpUtil.postReturnJson(PreferenceUtils
				.getString("address")
				+ "csmpv4/mb/getsdir.js"
				+ COMMON_PARAMETER
				+ "&d="
				+ paramString
				+ "&p="
				+ paramInt1
				+ "&f=" + paramInt2);
		if (localJSONObject != null)
			;
		try {
			String str = BASE64Util.byteToString(BASE64Util
					.decode(localJSONObject.getString("e")));
			LogUtil.e("其他错误信息:" + str);
			return;
		} catch (Exception localException) {
			localException.printStackTrace();
		}
	}

	public static List<BeifenDirectoryBean> getBeifenDirectoryList(
			String paramString1, String paramString2, String paramString3) {
		JSONObject localJSONObject1 = HttpUtil
				.postReturnJson(Config.SERVEL_ADDRESS + "csmpv4/mb/getbldir.js"
						+ COMMON_PARAMETER + "&d=" + paramString1 + "&p="
						+ paramString2 + "&f=" + paramString3);
		List<BeifenDirectoryBean> localArrayList = new ArrayList<BeifenDirectoryBean>();
		if (localJSONObject1 != null)
			try {
				String str1 = BASE64Util.byteToString(BASE64Util
						.decode(localJSONObject1.getString("e")));
				LogUtil.e("其他错误信息:" + str1);
				JSONArray localJSONArray = localJSONObject1.getJSONArray("d");
				for (int i = 0;; i++) {
					if (i >= localJSONArray.length())
						return localArrayList;
					BeifenDirectoryBean localBeifenDirectoryBean = new BeifenDirectoryBean();
					localBeifenDirectoryBean.setOtherMessage(str1);
					JSONObject localJSONObject2 = (JSONObject) localJSONArray
							.get(i);
					String str2 = BASE64Util.byteToString(BASE64Util
							.decode(localJSONObject2.getString("dn")));
					localBeifenDirectoryBean.setDirectoryName(str2);
					LogUtil.e("目录名:" + str2);
					String str3 = localJSONObject2.getString("fc");
					localBeifenDirectoryBean.setDirectoryMark2(str3);
					String str4 = BASE64Util.byteToString(BASE64Util
							.decode(str3));
					LogUtil.e("目录标示2:" + str4);
					String str5 = localJSONObject2.getString("dc");
					localBeifenDirectoryBean.setDirectoryMark1(str5);
					String str6 = BASE64Util.byteToString(BASE64Util
							.decode(str5));
					LogUtil.e("目录标示1:" + str6);
					localArrayList.add(localBeifenDirectoryBean);
				}
			} catch (Exception localException) {
			}
		return localArrayList;
	}

	public static List<BeifenFileBean> getBeifenFileList(String paramString1,
			String paramString2, String paramString3, String paramString4,
			String paramString5) {
		JSONObject localJSONObject1 = HttpUtil
				.postReturnJson(Config.SERVEL_ADDRESS + "csmpv4/mb/getbfdir.js"
						+ COMMON_PARAMETER + "&d=" + paramString1 + "&p="
						+ paramString2 + "&f=" + paramString3 + "&j="
						+ paramString4 + "&a=" + paramString5);
		List<BeifenFileBean> localArrayList = new ArrayList<BeifenFileBean>();
		if (localJSONObject1 != null)
			try {
				String str1 = BASE64Util.byteToString(BASE64Util
						.decode(localJSONObject1.getString("e")));
				LogUtil.e("其他错误信息:" + str1);
				JSONArray localJSONArray = localJSONObject1.getJSONArray("d");
				for (int i = 0;; i++) {
					if (i >= localJSONArray.length())
						return localArrayList;
					JSONObject localJSONObject2 = localJSONArray
							.getJSONObject(i);
					BeifenFileBean localBeifenFileBean = new BeifenFileBean();
					String str2 = BASE64Util.byteToString(BASE64Util
							.decode(localJSONObject2.getString("fn")));
					LogUtil.e("文件名:" + str2);
					localBeifenFileBean.setFileName(str2);
					localBeifenFileBean.setMake1(localJSONObject2
							.getString("fc"));
					localBeifenFileBean.setMake2(localJSONObject2
							.getString("fb"));
					localArrayList.add(localBeifenFileBean);
				}
			} catch (Exception localException) {
			}
		return localArrayList;
	}

	public static List<BeifenVersionBean> getBeifenVersionList(
			String paramString1, String paramString2, String paramString3) {
		JSONObject localJSONObject1 = HttpUtil
				.postReturnJson(Config.SERVEL_ADDRESS + "csmpv4/mb/getbvdir.js"
						+ COMMON_PARAMETER + "&d=" + paramString1 + "&p="
						+ paramString2 + "&f=" + paramString3);
		List<BeifenVersionBean> localArrayList = new ArrayList<BeifenVersionBean>();
		if (localJSONObject1 != null)
			try {
				String str1 = BASE64Util.byteToString(BASE64Util
						.decode(localJSONObject1.getString("e")));
				LogUtil.e("其他错误信息:" + str1);
				JSONArray localJSONArray = localJSONObject1.getJSONArray("d");
				for (int i = 0;; i++) {
					if (i >= localJSONArray.length())
						return localArrayList;
					JSONObject localJSONObject2 = localJSONArray
							.getJSONObject(i);
					BeifenVersionBean localBeifenVersionBean = new BeifenVersionBean();
					String str2 = localJSONObject2.getString("vb");
					localBeifenVersionBean.setMake2(str2);
					BASE64Util.byteToString(BASE64Util.decode(str2));
					String str3 = localJSONObject2.getString("vc");
					localBeifenVersionBean.setMake1(str3);
					BASE64Util.byteToString(BASE64Util.decode(str3));
					BASE64Util.byteToString(BASE64Util.decode(localJSONObject2
							.getString("vf")));
					String str4 = BASE64Util.byteToString(BASE64Util
							.decode(localJSONObject2.getString("vn")));
					localBeifenVersionBean.setVersionName(str4);
					LogUtil.e("版本名:" + str4);
					localArrayList.add(localBeifenVersionBean);
				}
			} catch (Exception localException) {
			}
		return localArrayList;
	}

	public static List<ShareBean> getShareList() {
		JSONObject localJSONObject1 = HttpUtil
				.postReturnJson(Config.SERVEL_ADDRESS + "csmpv4/mb/getsdir.js"
						+ COMMON_PARAMETER);
		List<ShareBean> localArrayList = new ArrayList<ShareBean>();
		if (localJSONObject1 != null)
			try {
				String str1 = BASE64Util.byteToString(BASE64Util
						.decode(localJSONObject1.getString("e")));
				LogUtil.e("其他错误信息:" + str1);
				JSONArray localJSONArray = localJSONObject1.getJSONArray("ps");
				for (int i = 0;; i++) {
					if (i >= localJSONArray.length())
						return localArrayList;
					JSONObject localJSONObject2 = localJSONArray
							.getJSONObject(i);
					ShareBean localShareBean = new ShareBean();
					String str2 = localJSONObject2.getString("pi");
					LogUtil.e("pi:" + str2);
					localShareBean.setFileId(str2);
					localJSONObject2.getString("pt");
					String str3 = BASE64Util.byteToString(BASE64Util
							.decode(localJSONObject2.getString("pd")));
					LogUtil.e("pd:" + str3);
					localShareBean.setFileName(str3);
					localArrayList.add(localShareBean);
				}
			} catch (JSONException localJSONException) {
				localJSONException.printStackTrace();
			}
		return localArrayList;
	}

	public static List<BeifenDirectoryBean> getShareDictory(
			String paramString1, String paramString2, String paramString3) {
		JSONObject localJSONObject1 = HttpUtil
				.postReturnJson(Config.SERVEL_ADDRESS + "csmpv4/mb/getsldir.js"
						+ COMMON_PARAMETER + "&d=" + paramString1 + "&p="
						+ paramString2 + "&f=" + paramString3);
		List<BeifenDirectoryBean> localArrayList = new ArrayList<BeifenDirectoryBean>();
		if (localJSONObject1 != null)
			try {
				String str1 = BASE64Util.byteToString(BASE64Util
						.decode(localJSONObject1.getString("e")));
				LogUtil.e("其他错误信息:" + str1);
				JSONArray localJSONArray = localJSONObject1.getJSONArray("d");
				for (int i = 0;; i++) {
					if (i >= localJSONArray.length())
						return localArrayList;
					BeifenDirectoryBean localBeifenDirectoryBean = new BeifenDirectoryBean();
					localBeifenDirectoryBean.setOtherMessage(str1);
					JSONObject localJSONObject2 = (JSONObject) localJSONArray
							.get(i);
					String str2 = BASE64Util.byteToString(BASE64Util
							.decode(localJSONObject2.getString("dn")));
					localBeifenDirectoryBean.setDirectoryName(str2);
					LogUtil.e("目录名:" + str2);
					String str3 = localJSONObject2.getString("fc");
					localBeifenDirectoryBean.setDirectoryMark2(str3);
					String str4 = BASE64Util.byteToString(BASE64Util
							.decode(str3));
					LogUtil.e("目录标示2:" + str4);
					String str5 = localJSONObject2.getString("dc");
					localBeifenDirectoryBean.setDirectoryMark1(str5);
					String str6 = BASE64Util.byteToString(BASE64Util
							.decode(str5));
					LogUtil.e("目录标示1:" + str6);
					localArrayList.add(localBeifenDirectoryBean);
				}
			} catch (Exception localException) {
			}
		return localArrayList;
	}

	public static List<ShareFileBean> getShareFile(String paramString1,
			String paramString2, String paramString3) {
		JSONObject localJSONObject1 = HttpUtil
				.postReturnJson(Config.SERVEL_ADDRESS + "csmpv4/mb/getsf.js"
						+ COMMON_PARAMETER + "&o=" + paramString1 + "&p="
						+ paramString2 + "&f=" + paramString3 + "&t=0");
		List<ShareFileBean> fileBeans = new ArrayList<ShareFileBean>();
		if (localJSONObject1 != null)
			try {
				String str1 = BASE64Util.byteToString(BASE64Util
						.decode(localJSONObject1.getString("e")));
				LogUtil.e("其他错误信息:" + str1);
				JSONArray jsonArray = localJSONObject1.getJSONArray("d");
				for (int i = 0; i < jsonArray.length(); i++) {
					ShareFileBean fileBean = new ShareFileBean();
					JSONObject json = (JSONObject) jsonArray.get(i);
					String fn = BASE64Util.byteToString(BASE64Util.decode(json
							.getString("fn")));
					fileBean.setFilename(fn);
					LogUtil.e("文件名:" + fn);
					String fc = json.getString("fc");
					fileBean.setMake1(fc);
					String fb = json.getString("fb");
					fileBean.setMake2(fb);
					// String str6 = BASE64Util.byteToString(BASE64Util
					// .decode(str5));
					// LogUtil.e("目录标示1:" + str6);
					fileBeans.add(fileBean);
				}
			} catch (Exception localException) {
			}
		return fileBeans;
	}

	public static UserMessageBean getUserInfo() {
		JSONObject localJSONObject = HttpUtil
				.postReturnJson(PreferenceUtils.getString("address")
						+ "csmpv4/mb/getui.js" + COMMON_PARAMETER);
		UserMessageBean localUserMessageBean = null;
		if (localJSONObject != null)
			localUserMessageBean = new UserMessageBean();
		try {
			String str1 = BASE64Util.byteToString(BASE64Util
					.decode(localJSONObject.getString("w")));
			LogUtil.e("用户Id:" + str1);
			localUserMessageBean.setUserId(str1);
			String str2 = BASE64Util.byteToString(BASE64Util
					.decode(localJSONObject.getString("e")));
			LogUtil.e("其他错误信息:" + str2);
			localUserMessageBean.setOtherMessage(str2);
			String str3 = BASE64Util.byteToString(BASE64Util
					.decode(localJSONObject.getString("udc")));
			LogUtil.e("所在企业:" + str3);
			localUserMessageBean.setCompany(str3);
			String str4 = BASE64Util.byteToString(BASE64Util
					.decode(localJSONObject.getString("udi")));
			LogUtil.e("所在部门id:" + str4);
			localUserMessageBean.setDepartmentId(str4);
			String str5 = BASE64Util.byteToString(BASE64Util
					.decode(localJSONObject.getString("udn")));
			LogUtil.e("所在部门名:" + str5);
			localUserMessageBean.setDepartmentName(str5);
			String str6 = BASE64Util.byteToString(BASE64Util
					.decode(localJSONObject.getString("ut")));
			LogUtil.e("用户类型:" + str6);
			localUserMessageBean.setUserType(str6);
			String str7 = BASE64Util.byteToString(BASE64Util
					.decode(localJSONObject.getString("usc")));
			LogUtil.e("用户已使用容量:" + str7);
			localUserMessageBean.setCapacity(str7);
			String str8 = BASE64Util.byteToString(BASE64Util
					.decode(localJSONObject.getString("upb")));
			LogUtil.e("最后一次备份时间:" + str8);
			localUserMessageBean.setLastBackupTime(str8);
			return localUserMessageBean;
		} catch (JSONException localJSONException) {
			localJSONException.printStackTrace();
		}
		return localUserMessageBean;
	}

	/**
	 * 获取文件保险箱列表
	 * 
	 * @param paramString1
	 * @param paramString2
	 * @return
	 */
	public static List<FileBoxBean> getFileBox(String paramString1,
			String paramString2) {

		String address = Config.SERVEL_ADDRESS + ServerInterface.FILEBOX
				+ COMMON_PARAMETER + "&s=" + paramString1 + "&v="
				+ paramString2;
		List<FileBoxBean> boxBeans = new ArrayList<FileBoxBean>();
		JSONObject jsonObject = HttpUtil.postReturnJson(address);
		try {
			String str1 = BASE64Util.byteToString(BASE64Util.decode(jsonObject
					.getString("e")));
			LogUtil.e("其他错误信息:" + str1);
			if (str1.equals("ok")) {
				JSONArray jsonArray = jsonObject.getJSONArray("d");
				for (int i = 0; i < jsonArray.length(); i++) {
					JSONObject json = jsonArray.getJSONObject(i);
					FileBoxBean boxBean = new FileBoxBean();
					// 文件名
					String fn = json.getString("fn");
					fn = BASE64Util.byteToString(BASE64Util.decode(fn));
					boxBean.setFilename(fn);

					String fc = json.getString("fc");
					LogUtil.e("fc:" + fc);
					boxBean.setMaker1(fc);

					String fb = json.getString("fb");
					LogUtil.e("fb:" + fb);
					boxBean.setMaker2(fb);
					boxBeans.add(boxBean);
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return boxBeans;
	}

	public static String downFile(String paramString1, String paramString2,
			String paramString3) {
		return HttpUtil.postDocumentReturnResult(Config.SERVEL_ADDRESS
				+ "csmpv4/mb/getf.js" + COMMON_PARAMETER + "&j=" + paramString1
				+ "&t=" + paramString2, paramString3);
	}

}
